﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace UbbTimetable2
{
    /// <summary>
    /// A struct to store building data.
    /// </summary>
    struct Building
    {
        int _buildingId;
        String _buildingAddress;
        String _buildingTel;

        public String BuildingAddress
        {
            get { return _buildingAddress; }
            set { _buildingAddress = value; }
        }

        public String BuildingTel
        {
            get { return _buildingTel; }
            set { _buildingTel = value; }
        }

        public int BuildingId
        {
            get { return _buildingId; }
            set { _buildingId = value; }
        }


    }

    /// <summary>
    /// A DAO that handles building data.
    /// </summary>
    class BuildingDao : Dao
    {
        private String err = "";

        /// <summary>
        /// The last error message.
        /// </summary>
        public String Err
        {
            get { return err; }
        }

        /// <summary>
        /// Returns the list of buildings.
        /// </summary>
        /// <returns>the list of buildings.</returns>
        public List<Building> GetBuldingList()
        {
            String query = "SELECT * FROM Epulet";
            SqlDataReader rdr = ExecuteReader(query, ref err);

            List<Building> list = new List<Building>();
            if (err == "OK")
            {
                while (rdr.Read())
                {
                    Building item = new Building();
                    item.BuildingId = Convert.ToInt32(rdr[0]);
                    item.BuildingAddress = rdr[1].ToString().TrimEnd();
                    item.BuildingTel = rdr[2].ToString().TrimEnd();

                    list.Add(item);
                }
            }
            CloseDataReader(rdr);
            return list;
        }
    }
}
